# read in data
library(stringr)
library(data.table)
library(ggplot2)
library(dplyr)
library(lubridate)
library(broom)

################ set your wd
setwd()
##############

FB_Preprocessed_Local <- readRDS("~Data/FB_Preprocessed_Local.rds")
FBlinks_Preprocessed_Local <- readRDS("~Data/FBlinks_Preprocessed_Local.rds")
Tweets_Preprocessed_Local <- readRDS("~Data/Tweets_Preprocessed_Local.rds")
Tweetslinks_Preprocessed_Local <- readRDS("~Data/Tweetslinks_Preprocessed_Local.rds")

FB_Preprocessed_Local$party <- ifelse(FB_Preprocessed_Local$party == "R", "R", "D")
FBlinks_Preprocessed_Local$party <- ifelse(FBlinks_Preprocessed_Local$party == "R", "R", "D")
Tweets_Preprocessed_Local$party <- ifelse(Tweets_Preprocessed_Local$party == "R", "R", "D")
Tweetslinks_Preprocessed_Local$party <- ifelse(Tweetslinks_Preprocessed_Local$party == "R", "R", "D")

##################

FBgroup <- FB_Preprocessed_Local %>%
  group_by(party, year, month) %>%
  dplyr::summarize(
    Local = mean(Local)*100
  ) %>%
  ungroup()

FBgroup$Date <- paste(FBgroup$year, FBgroup$month, "1", sep="-")

FBgroup$Measure <- "Message"
FBgroup$Platform <- "Facebook"

FBlinksgroup <- FBlinks_Preprocessed_Local %>%
  group_by(party, year, month) %>%
  dplyr::summarize(
    Local = mean(Locallink)*100
  ) %>%
  ungroup()

FBlinksgroup$Date <- paste(FBlinksgroup$year, FBlinksgroup$month, "1", sep="-")

FBlinksgroup$Measure <- "Links"
FBlinksgroup$Platform <- "Facebook"

tweetsgroup <- Tweets_Preprocessed_Local %>%
  group_by(party, year, month) %>%
  dplyr::summarize(
    Local = mean(local)*100
  ) %>%
  ungroup()

tweetsgroup$Date <- paste(tweetsgroup$year, tweetsgroup$month, "1", sep="-")

tweetsgroup$Measure <- "Message"
tweetsgroup$Platform <- "Twitter"

tweetslinksgroup <- Tweetslinks_Preprocessed_Local %>%
  group_by(party, year, month) %>%
  dplyr::summarize(
    Local = mean(Locallink)*100
  ) %>%
  ungroup()

tweetslinksgroup$Date <- paste(tweetslinksgroup$year, tweetslinksgroup$month, "1", sep="-")

tweetslinksgroup$Measure <- "Links"
tweetslinksgroup$Platform <- "Twitter"

####

FBgroupnop <- FB_Preprocessed_Local %>%
  group_by(year, month) %>%
  dplyr::summarize(
    Local = mean(Local)*100
  ) %>%
  ungroup()

FBgroupnop$Date <- paste(FBgroupnop$year, FBgroupnop$month, "1", sep="-")

FBgroupnop$Measure <- "Message"
FBgroupnop$Platform <- "Facebook"
FBgroupnop$party <- "All"

FBlinksgroupnop <- FBlinks_Preprocessed_Local %>%
  group_by(year, month) %>%
  dplyr::summarize(
    Local = mean(Locallink)*100
  ) %>%
  ungroup()

FBlinksgroupnop$Date <- paste(FBlinksgroupnop$year, FBlinksgroupnop$month, "1", sep="-")

FBlinksgroupnop$Measure <- "Links"
FBlinksgroupnop$Platform <- "Facebook"
FBlinksgroupnop$party <- "All"

tweetsgroupnop <- Tweets_Preprocessed_Local %>%
  group_by(year, month) %>%
  dplyr::summarize(
    Local = mean(local)*100
  ) %>%
  ungroup()

tweetsgroupnop$Date <- paste(tweetsgroupnop$year, tweetsgroupnop$month, "1", sep="-")

tweetsgroupnop$Measure <- "Message"
tweetsgroupnop$Platform <- "Twitter"
tweetsgroupnop$party <- "All"

tweetslinksgroupnop <- Tweetslinks_Preprocessed_Local %>%
  group_by(year, month) %>%
  dplyr::summarize(
    Local = mean(Locallink)*100
  ) %>%
  ungroup()

tweetslinksgroupnop$Date <- paste(tweetslinksgroupnop$year, tweetslinksgroupnop$month, "1", sep="-")

tweetslinksgroupnop$Measure <- "Links"
tweetslinksgroupnop$Platform <- "Twitter"
tweetslinksgroupnop$party <- "All"

#######################

combined <- rbind(FBgroup, FBgroupnop, FBlinksgroup, FBlinksgroupnop, tweetsgroup, tweetsgroupnop, tweetslinksgroup, tweetslinksgroupnop)
combined$Date <- as.Date(combined$Date)
combined <- filter(combined, year > 2010)
combined$Party <- combined$party

##############

combined$Party <- ifelse(combined$Party == "D", "Democrats", combined$Party)
combined$Party <- ifelse(combined$Party == "R", "Republicans", combined$Party)

ggplot(combined, aes(x=Date, y=Local, color=Party, shape=Party)) +
  geom_point(size = 0.7, alpha = 0.8) +
  geom_smooth(size = 0.8, se=FALSE) +
  scale_color_manual(values = c("grey70", "blue3", "red3")) +
  geom_vline(xintercept = as.numeric(as.Date("2012-11-01")), linetype="dotted", 
             color = "black", size=1) +
  geom_vline(xintercept = as.numeric(as.Date("2016-11-01")), linetype="dotted", 
             color = "black", size=1) +
  geom_vline(xintercept = as.numeric(as.Date("2020-11-01")), linetype="dotted", 
             color = "black", size=1) +
  ylab("Localized Messages (Monthly Percentage)") +
  theme_bw() +
  theme(legend.position="bottom",
        legend.text = element_text(size=12), 
        legend.title = element_text(size=12)) +
  ylim(0,75) +
  facet_wrap(~Measure + Platform)+
  scale_x_date(date_breaks = "years" , date_labels = "%Y")+ 
  theme(axis.text.x = element_text(angle = 30, hjust=0.5))

ggsave(file = "Local_Party_Combined.png", height = 9, width = 8)





##################################
######################################## appendix no major markets
########################

FB_Preprocessed_Local <- readRDS("~Data/FB_Preprocessed_Local.rds")
FBlinks_Preprocessed_Local <- readRDS("~Data/FBlinks_Preprocessed_Local.rds")
Tweets_Preprocessed_Local <- readRDS("~Data/Tweets_Preprocessed_Local.rds")
Tweetslinks_Preprocessed_Local <- readRDS("~Data/Tweetslinks_Preprocessed_Local.rds")

FB_Preprocessed_Local$party <- ifelse(FB_Preprocessed_Local$party == "R", "R", "D")
FBlinks_Preprocessed_Local$party <- ifelse(FBlinks_Preprocessed_Local$party == "R", "R", "D")
Tweets_Preprocessed_Local$party <- ifelse(Tweets_Preprocessed_Local$party == "R", "R", "D")
Tweetslinks_Preprocessed_Local$party <- ifelse(Tweetslinks_Preprocessed_Local$party == "R", "R", "D")

FB_Preprocessed_Local <- filter(FB_Preprocessed_Local, !state %in% c("VA", "NJ", "NY", "MD", "CA"))
FBlinks_Preprocessed_Local <- filter(FBlinks_Preprocessed_Local, !state %in% c("VA", "NJ", "NY", "MD", "CA"))
Tweets_Preprocessed_Local <- filter(Tweets_Preprocessed_Local, !state %in% c("VA", "NJ", "NY", "MD", "CA"))
Tweetslinks_Preprocessed_Local <- filter(Tweetslinks_Preprocessed_Local, !state %in% c("VA", "NJ", "NY", "MD", "CA"))

##################

FBgroup <- FB_Preprocessed_Local %>%
  group_by(party, year, month) %>%
  dplyr::summarize(
    Local = mean(Local)*100
  ) %>%
  ungroup()

FBgroup$Date <- paste(FBgroup$year, FBgroup$month, "1", sep="-")

FBgroup$Measure <- "Message"
FBgroup$Platform <- "Facebook"

FBlinksgroup <- FBlinks_Preprocessed_Local %>%
  group_by(party, year, month) %>%
  dplyr::summarize(
    Local = mean(Locallink)*100
  ) %>%
  ungroup()

FBlinksgroup$Date <- paste(FBlinksgroup$year, FBlinksgroup$month, "1", sep="-")

FBlinksgroup$Measure <- "Links"
FBlinksgroup$Platform <- "Facebook"

tweetsgroup <- Tweets_Preprocessed_Local %>%
  group_by(party, year, month) %>%
  dplyr::summarize(
    Local = mean(local)*100
  ) %>%
  ungroup()

tweetsgroup$Date <- paste(tweetsgroup$year, tweetsgroup$month, "1", sep="-")

tweetsgroup$Measure <- "Message"
tweetsgroup$Platform <- "Twitter"

tweetslinksgroup <- Tweetslinks_Preprocessed_Local %>%
  group_by(party, year, month) %>%
  dplyr::summarize(
    Local = mean(Locallink)*100
  ) %>%
  ungroup()

tweetslinksgroup$Date <- paste(tweetslinksgroup$year, tweetslinksgroup$month, "1", sep="-")

tweetslinksgroup$Measure <- "Links"
tweetslinksgroup$Platform <- "Twitter"

####

FBgroupnop <- FB_Preprocessed_Local %>%
  group_by(year, month) %>%
  dplyr::summarize(
    Local = mean(Local)*100
  ) %>%
  ungroup()

FBgroupnop$Date <- paste(FBgroupnop$year, FBgroupnop$month, "1", sep="-")

FBgroupnop$Measure <- "Message"
FBgroupnop$Platform <- "Facebook"
FBgroupnop$party <- "All"

FBlinksgroupnop <- FBlinks_Preprocessed_Local %>%
  group_by(year, month) %>%
  dplyr::summarize(
    Local = mean(Locallink)*100
  ) %>%
  ungroup()

FBlinksgroupnop$Date <- paste(FBlinksgroupnop$year, FBlinksgroupnop$month, "1", sep="-")

FBlinksgroupnop$Measure <- "Links"
FBlinksgroupnop$Platform <- "Facebook"
FBlinksgroupnop$party <- "All"

tweetsgroupnop <- Tweets_Preprocessed_Local %>%
  group_by(year, month) %>%
  dplyr::summarize(
    Local = mean(local)*100
  ) %>%
  ungroup()

tweetsgroupnop$Date <- paste(tweetsgroupnop$year, tweetsgroupnop$month, "1", sep="-")

tweetsgroupnop$Measure <- "Message"
tweetsgroupnop$Platform <- "Twitter"
tweetsgroupnop$party <- "All"

tweetslinksgroupnop <- Tweetslinks_Preprocessed_Local %>%
  group_by(year, month) %>%
  dplyr::summarize(
    Local = mean(Locallink)*100
  ) %>%
  ungroup()

tweetslinksgroupnop$Date <- paste(tweetslinksgroupnop$year, tweetslinksgroupnop$month, "1", sep="-")

tweetslinksgroupnop$Measure <- "Links"
tweetslinksgroupnop$Platform <- "Twitter"
tweetslinksgroupnop$party <- "All"

#######################

combined <- rbind(FBgroup, FBgroupnop, FBlinksgroup, FBlinksgroupnop, tweetsgroup, tweetsgroupnop, tweetslinksgroup, tweetslinksgroupnop)
combined$Date <- as.Date(combined$Date)
combined <- filter(combined, year > 2010)
combined$Party <- combined$party

##############

combined$Party <- ifelse(combined$Party == "D", "Democrats", combined$Party)
combined$Party <- ifelse(combined$Party == "R", "Republicans", combined$Party)

ggplot(combined, aes(x=Date, y=Local, color=Party, shape=Party)) +
  geom_point(size = 0.7, alpha = 0.8) +
  geom_smooth(size = 0.8, se=FALSE) +
  scale_color_manual(values = c("grey70", "blue3", "red3")) +
  geom_vline(xintercept = as.numeric(as.Date("2012-11-01")), linetype="dotted", 
             color = "black", size=1) +
  geom_vline(xintercept = as.numeric(as.Date("2016-11-01")), linetype="dotted", 
             color = "black", size=1) +
  geom_vline(xintercept = as.numeric(as.Date("2020-11-01")), linetype="dotted", 
             color = "black", size=1) +
  ylab("Localized Messages (Monthly Percentage)") +
  theme_bw() +
  theme(legend.position="bottom",
        legend.text = element_text(size=12), 
        legend.title = element_text(size=12)) +
  ylim(0,75) +
  facet_wrap(~Measure + Platform)+
  scale_x_date(date_breaks = "years" , date_labels = "%Y")+ 
  theme(axis.text.x = element_text(angle = 30, hjust=0.5))

ggsave(file = "Local_Party_Combined RR nomajor.png", height = 9, width = 8)



##################################
######################################## appendix no retweets
########################

FB_Preprocessed_Local <- readRDS("~Data/FB_Preprocessed_Local.rds")
FBlinks_Preprocessed_Local <- readRDS("~Data/FBlinks_Preprocessed_Local.rds")
Tweets_Preprocessed_Local <- readRDS("~Data/Tweets_Preprocessed_Local.rds")
Tweetslinks_Preprocessed_Local <- readRDS("~Data/Tweetslinks_Preprocessed_Local.rds")

FB_Preprocessed_Local$party <- ifelse(FB_Preprocessed_Local$party == "R", "R", "D")
FBlinks_Preprocessed_Local$party <- ifelse(FBlinks_Preprocessed_Local$party == "R", "R", "D")
Tweets_Preprocessed_Local$party <- ifelse(Tweets_Preprocessed_Local$party == "R", "R", "D")
Tweetslinks_Preprocessed_Local$party <- ifelse(Tweetslinks_Preprocessed_Local$party == "R", "R", "D")

Tweets_Preprocessed_Local <- Tweets_Preprocessed_Local %>%
  mutate(starts_with_RT = ifelse(str_starts(text, "RT "), 1, 0))
Tweetslinks_Preprocessed_Local <- Tweetslinks_Preprocessed_Local %>%
  mutate(starts_with_RT = ifelse(str_starts(text, "RT "), 1, 0))

Tweets_Preprocessed_Local <- filter(Tweets_Preprocessed_Local, starts_with_RT == 0)
Tweetslinks_Preprocessed_Local <- filter(Tweetslinks_Preprocessed_Local, starts_with_RT == 0)

##################

FBgroup <- FB_Preprocessed_Local %>%
  group_by(party, year, month) %>%
  dplyr::summarize(
    Local = mean(Local)*100
  ) %>%
  ungroup()

FBgroup$Date <- paste(FBgroup$year, FBgroup$month, "1", sep="-")

FBgroup$Measure <- "Message"
FBgroup$Platform <- "Facebook"

FBlinksgroup <- FBlinks_Preprocessed_Local %>%
  group_by(party, year, month) %>%
  dplyr::summarize(
    Local = mean(Locallink)*100
  ) %>%
  ungroup()

FBlinksgroup$Date <- paste(FBlinksgroup$year, FBlinksgroup$month, "1", sep="-")

FBlinksgroup$Measure <- "Links"
FBlinksgroup$Platform <- "Facebook"

tweetsgroup <- Tweets_Preprocessed_Local %>%
  group_by(party, year, month) %>%
  dplyr::summarize(
    Local = mean(local)*100
  ) %>%
  ungroup()

tweetsgroup$Date <- paste(tweetsgroup$year, tweetsgroup$month, "1", sep="-")

tweetsgroup$Measure <- "Message"
tweetsgroup$Platform <- "Twitter"

tweetslinksgroup <- Tweetslinks_Preprocessed_Local %>%
  group_by(party, year, month) %>%
  dplyr::summarize(
    Local = mean(Locallink)*100
  ) %>%
  ungroup()

tweetslinksgroup$Date <- paste(tweetslinksgroup$year, tweetslinksgroup$month, "1", sep="-")

tweetslinksgroup$Measure <- "Links"
tweetslinksgroup$Platform <- "Twitter"

####

FBgroupnop <- FB_Preprocessed_Local %>%
  group_by(year, month) %>%
  dplyr::summarize(
    Local = mean(Local)*100
  ) %>%
  ungroup()

FBgroupnop$Date <- paste(FBgroupnop$year, FBgroupnop$month, "1", sep="-")

FBgroupnop$Measure <- "Message"
FBgroupnop$Platform <- "Facebook"
FBgroupnop$party <- "All"

FBlinksgroupnop <- FBlinks_Preprocessed_Local %>%
  group_by(year, month) %>%
  dplyr::summarize(
    Local = mean(Locallink)*100
  ) %>%
  ungroup()

FBlinksgroupnop$Date <- paste(FBlinksgroupnop$year, FBlinksgroupnop$month, "1", sep="-")

FBlinksgroupnop$Measure <- "Links"
FBlinksgroupnop$Platform <- "Facebook"
FBlinksgroupnop$party <- "All"

tweetsgroupnop <- Tweets_Preprocessed_Local %>%
  group_by(year, month) %>%
  dplyr::summarize(
    Local = mean(local)*100
  ) %>%
  ungroup()

tweetsgroupnop$Date <- paste(tweetsgroupnop$year, tweetsgroupnop$month, "1", sep="-")

tweetsgroupnop$Measure <- "Message"
tweetsgroupnop$Platform <- "Twitter"
tweetsgroupnop$party <- "All"

tweetslinksgroupnop <- Tweetslinks_Preprocessed_Local %>%
  group_by(year, month) %>%
  dplyr::summarize(
    Local = mean(Locallink)*100
  ) %>%
  ungroup()

tweetslinksgroupnop$Date <- paste(tweetslinksgroupnop$year, tweetslinksgroupnop$month, "1", sep="-")

tweetslinksgroupnop$Measure <- "Links"
tweetslinksgroupnop$Platform <- "Twitter"
tweetslinksgroupnop$party <- "All"

#######################

combined <- rbind(FBgroup, FBgroupnop, FBlinksgroup, FBlinksgroupnop, tweetsgroup, tweetsgroupnop, tweetslinksgroup, tweetslinksgroupnop)
combined$Date <- as.Date(combined$Date)
combined <- filter(combined, year > 2010)
combined$Party <- combined$party

##############

combined$Party <- ifelse(combined$Party == "D", "Democrats", combined$Party)
combined$Party <- ifelse(combined$Party == "R", "Republicans", combined$Party)

ggplot(combined, aes(x=Date, y=Local, color=Party, shape=Party)) +
  geom_point(size = 0.7, alpha = 0.8) +
  geom_smooth(size = 0.8, se=FALSE) +
  scale_color_manual(values = c("grey70", "blue3", "red3")) +
  geom_vline(xintercept = as.numeric(as.Date("2012-11-01")), linetype="dotted", 
             color = "black", size=1) +
  geom_vline(xintercept = as.numeric(as.Date("2016-11-01")), linetype="dotted", 
             color = "black", size=1) +
  geom_vline(xintercept = as.numeric(as.Date("2020-11-01")), linetype="dotted", 
             color = "black", size=1) +
  ylab("Localized Messages (Monthly Percentage)") +
  theme_bw() +
  theme(legend.position="bottom",
        legend.text = element_text(size=12), 
        legend.title = element_text(size=12)) +
  ylim(0,75) +
  facet_wrap(~Measure + Platform)+
  scale_x_date(date_breaks = "years" , date_labels = "%Y")+ 
  theme(axis.text.x = element_text(angle = 30, hjust=0.5))

ggsave(file = "Local_Party_Combined RR noRT.png", height = 9, width = 8)
